package com.nanbei.queue;

import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 * @ClassDescription:
 * @JdkVersion: 17
 * @Author: BaoShan.li
 * @Created: 2023/11/28 14:59
 */
public class DequeTest {


    @Test
    public void test(){
        LinkDeque<Integer> integers = new LinkDeque<>(4);
        integers.offerFirst(2);
        integers.offerFirst(1);
        integers.offerLast(3);
        integers.offerLast(4);
        integers.offerLast(5);

        prin(integers);

        System.out.println(integers.pollFirst());
        System.out.println(integers.pollLast());

        System.out.println("-------");
        prin(integers);

        System.out.println(integers.peekFirst());
        System.out.println(integers.peekLast());
        System.out.println("-------");
        prin(integers);


    }

    @Test
    public void test2(){
        ArrayDeque<Integer> integers = new ArrayDeque<>(4);
        integers.offerFirst(2);
        integers.offerFirst(1);
        integers.offerLast(3);
        integers.offerLast(4);
        integers.offerLast(5);

        prin(integers);

        System.out.println(integers.pollFirst());
        System.out.println(integers.pollLast());

        System.out.println("-------");
        prin(integers);

        System.out.println(integers.peekFirst());
        System.out.println(integers.peekLast());
        System.out.println("-------");
        prin(integers);


    }

    @Test
    public void test3(){
        DoubleBlockingQueue<Integer> blockingQueue = new DoubleBlockingQueue<>(3);

        new Thread(()->{
            try {
                blockingQueue.offer(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"offer").start();

        new Thread(()->{
            try {
                blockingQueue.poll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"poll").start();


        try {
            TimeUnit.MILLISECONDS.sleep(200);
            System.out.println("11111");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    private void prin(ArrayDeque<Integer> integers) {
        for (Integer integer : integers) {
            System.out.println(integer);
        }
    }


    private void prin(LinkDeque<Integer> integers) {
        for (Integer integer : integers) {
            System.out.println(integer);
        }
    }
}
